<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="24">
          <a-form-item label="退款银行类型" v-bind="validateInfos.refundBankType">
            <j-dict-select-tag v-model:value="formData.refundBankType" dictCode="TKLX" placeholder="请选择退款银行类型" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="付款人账号" v-bind="validateInfos.payAccount">
            <a-input v-model:value="formData.payAccount" placeholder="请输入付款人账号" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="付款人名称" v-bind="validateInfos.payAccountName">
            <a-input v-model:value="formData.payAccountName" placeholder="请输入付款人名称" :disabled="true"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="退款金额" v-bind="validateInfos.amount">
            <a-input-number v-model:value="formData.amount" placeholder="请输入退款金额" style="width: 100%" :disabled="true"/>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="退款原因" v-bind="validateInfos.reson">
            <a-input v-model:value="formData.reson" placeholder="请输入退款原因" :disabled="disabled" ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="短信验证码" v-bind="validateInfos.smsCode" :disabled="disabled">
            <a-row>
              <a-col :span="20">
                <a-input v-model:value="formData.smsCode" placeholder="请输入短信验证码" ></a-input>
              </a-col>
              <a-col :span="4">
                <a-button type="primary" @click="doSmsCode">验证码</a-button>
              </a-col>
            </a-row>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="联行号查询">
            <a-row>
              <a-row>
                <a-col :span="20">
                  <a-input v-model:value="formData.queryBanck" placeholder="请输入要查询的联行号" :disabled="disabled"></a-input>
                </a-col>
                <a-col :span="4">
                  <a-button type="primary" @click="selectOnSearch">查询</a-button>
                </a-col>
              </a-row>
            </a-row>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="联行号选择" v-bind="validateInfos.unionBankNo">
            <a-row>
              <a-select v-model:value="formData.unionBankNo" :options="bankOptions" :fieldNames="{label:'bankName',value:'bankNo'}" @change="bankSelectChage"/>
            </a-row>
          </a-form-item>
          <a-input v-show =false  v-model:value="formData.unionBankName"></a-input>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import { getValueType } from '/@/utils';
  import { refund } from '../../../bank/InsuranceHangzhouBack.api';
  import { directRefundSmsCode,bankQuery } from '/@/views/insurance/refund/InsuranceRefund.api.ts';
  import { Form } from 'ant-design-vue';
  let queryBanck =ref();
  interface Option {
    text: string;
    value: string;
  }
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const bankOptions = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    unionBankNo: '',
    unionBankName: '',
    smsCode: '',
    reson: '',
    amount: '',
    payAccount: '',
    payAccountName: '',
    queryBanck:'',
    refundBankType: '',
  });
  const { createMessage } = useMessage();1
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    smsCode: [{ required: true, message: '请输入短信验证码!'},],
    refundBankType: [{ required: true, message: '请输入退款银行类型!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  function selectOnSearch() {
    bankQueryAjax(formData.queryBanck);
  }

  function bankSelectChage(value, option){
    formData.unionBankName = option.bankName;
    formData.unionBankNo = option.bankNo;
  }

  function bankQueryAjax(val){
    console.info(val)
    bankQuery({'bankName':val})
      .then((res) => {
        bankOptions.value = res
      })
  }

  /**
   * 新增
   */
  function add() {
    edit({});
  }


  /**
   * 获取短信验证码
   */
  async function doSmsCode(){
    await directRefundSmsCode({'id':formData.id})
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      console.info(record);
      //赋值
      formData.id = record.id;
      formData.amount = record.loanamount;
      formData.payAccount = record.recAccount;
      formData.payAccountName = record.recAccountName;
      formData.queryBanck = record.partyBankName;
      if(record.partyBankName!=null){
        let type = record.partyBankName.search('杭州银行') == -1 ? 'HW' : 'HN';
        formData.refundBankType = type;
      }
    });
  }


  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await refund(model)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    min-height: 500px !important;
    overflow-y: auto;
    padding: 24px 24px 24px 24px;
  }
</style>
